package sample;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lq
 * 创建时间 2019/4/12 0:46
 **/
public class Main extends Application {
    ImageView imageView = new ImageView();

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        TextField inputText = new TextField("C:/Users/lq/Pictures/ps");
        //button
        Button btChooseFile = new Button("选择图片");
        Label pathLab = new Label("file Path ");

        // Create a button to choose a file
        Button nextBtn = new Button("AI识别");

        // Create a button to choose a directory
        Button btChooseDirectory = new Button("Choose Folder");


        imageView.setFitHeight(500);
        imageView.setFitWidth(400);
        final List<String> images = new ArrayList<>();

        //计数器
        int[] clickCount = {0};

        nextBtn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                Alert alert = new Alert(Alert.AlertType.NONE);

                alert.setTitle("识别结果");
                alert.setContentText("hello world");

                ButtonType bt = new ButtonType("OK");
                alert.getButtonTypes().add(bt);


                alert.showAndWait();

            }
        });

        btChooseFile.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                FileChooser fileChooser = new FileChooser();
                fileChooser.setTitle("Choose File");
                fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("JPG Files", "*.jpg"));
                fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("PNG Files", "*.png"));
                fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("BMP Files", "*.bmp"));
                File file = fileChooser.showOpenDialog(primaryStage);
                if (file != null) {
                    System.out.println(file.getAbsolutePath());
                    try {
                        initImageView(file.getAbsolutePath());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }



            }
        });


        VBox vbox = new VBox(inputText, nextBtn, imageView, btChooseFile);
        vbox.setMaxSize(600, 600);
        vbox.setMinSize(400, 600);
        Scene scene = new Scene(vbox);
        primaryStage.setTitle("简易图片查看器");
        primaryStage.setScene(scene);
        primaryStage.show();
    }


    public ImageView initImageView(String imagePath) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(imagePath);
        Image image = new Image(fileInputStream);

        imageView.setImage(image);
        fileInputStream.close();
        return imageView;
    }

    /**
     * 获取父路径
     *
     * @param parentPath
     * @return
     */
    public String[] getImageNames(String parentPath) throws URISyntaxException {
        File parentDir = new File(parentPath);
        String[] pngs = parentDir.list(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                if (name.endsWith("jpg")) {
                    System.out.println(name);
                    return true;
                }
                return false;
            }
        });
        System.out.println(pngs.length);
        return pngs;
    }


}
